package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DAL.Entites.Recommendation;
import DAL.Entites.User;
import DAL.JDBC.DBConn;
import DAL.JDBC.RecommendationJDBC;
import DAL.JDBC.UserJDBC;

public class GetRecommendation extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public GetRecommendation() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject result = new JSONObject();
		RecommendationJDBC recommendationJDBC = new RecommendationJDBC(DBConn.getConnection());
		UserJDBC userJDBC = new UserJDBC(DBConn.getConnection());
		
		int gameId = Integer.parseInt(request.getParameter("gameId"));
		
		List<Recommendation> recs = recommendationJDBC.listRecommendationsByGameId(gameId);
		for (Recommendation recommendation : recs) {
			User user = userJDBC.getUserById(recommendation.getUserId());
			recommendation.setUserFirstName(user.getFirstName());
			recommendation.setUserLastName(user.getLastName());
		}
		try {
			result.put("recommendations", recs);
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
		
		PrintWriter pw = response.getWriter();
		pw.write(result.toString());
	}

}
